package com.Day4;

import java.util.ArrayList;

import java.util.List;
import java.sql.ClientInfoStatus;
import java.util.Collection;
import com.sun.deploy.util.StringUtils;

public class DynamicList implements CommonList{
    //常量
    private static final int INIT_SIZE = 16;//只有这个类才能使用;Static ——静态不用实例化就能访问;final ——不可变;这样写便于代码维护！
    private int[] arr;
    private int currIndex;


    //构造函数
    public DynamicList(int size){
        arr = new int[size];
        currIndex = 0;
    }

    public DynamicList(){
        arr = new int[INIT_SIZE];
    }


    //头部添加数据
    public void addFirst(int number){
        if(isOutBound()){
            scaleSize();
        }
        for (int i = 1; i < arr.length; i++){
            arr[i] = arr[i+1];
            arr[0] = number;
        }
    }


    //末尾添加数据（不定长）
    public void addLast(int number){
        if(isOutBound()){
            scaleSize();
        }
        arr[currIndex] = number;
        currIndex++;

    }


    //往特定index添加数据
    public void addIndex(int index, int number){
        if (index > currIndex){
            return;
        }
        if (isOutBound()){
            scaleSize();
        }
        for (int i = currIndex; i > index; i--){
            arr[i] = arr[i-1];
        }
        arr[index] = number;
        currIndex++;
    }

    //末尾删除
    public void remove(){
        arr[currIndex] = 0;
        currIndex--;
    }

    //特定位置删除
    public void removeIndex(int index){
        if (index > currIndex){
            return;
        }
        for (int i = currIndex; i > index; i--){
            arr[i] = arr[i+1];
            currIndex--;
        }
    }

    //头部删除
    public void removeFirst(){
        for (int i = 1; i <arr.length; i++){
            arr[i] = arr[i+1];
        }
    }

    //缩容
    public void reduceArr(){
        double percent = currIndex / arr.length;

        if (percent < 0.4){
            int[] newArr = new int[arr.length / 2];
            for (int i = 0; i < currIndex; i++){
                newArr[i] = arr[i];
            }
        }
    }


    //获取实际大小
    public int getSize(){
        return currIndex;
    }


    //判断是否超过数组长度函数
    public boolean isOutBound(){
        return currIndex >= arr.length;//不用二次判断了
    }


    //扩容一位函数
    public void scaleSize(){
        //n -> 2n
        int[] newArr = new int[2*arr.length];
        for (int i = 0; i < arr.length; i++){
            newArr[i] = arr[i];
        }
        arr = newArr;
    }


    @Override
    public boolean isEmpty() {
        return currIndex ==0;
    }


    @Override
    public boolean isContains(int i) {
        for (int j = 0; j < currIndex; j++) {
            if(i == arr[j]){
                return true;
            }
        }
        return false;
    }


    @Override
    public int[] toArray() {
        return arr;
    }


    @Override
    public void clear() {
        for (int i = 0; i <currIndex; i++){
            arr[i] = arr[0];
        }
    }


    //获取当前容量
    public int getCapability(){
        return arr.length - currIndex;
    }

    //返回字符串
    public String toString(){
        String word = " " + String.valueOf(arr[0]);
        for (int i = 1; i <currIndex; i++){
            word = word + " " + String.valueOf(arr[i]);
        }
        return word;
    }


    //出队
    public int getLast(){
        return arr[currIndex];
    }

    //出栈
    public int getFirst(){
        return arr[0];
    }

}
